<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 {{ total }} 条记录</span>
          <el-button
            @click="openDrawer('add')"
            icon="el-icon-plus"
            size="small"
            type="primary"
            round
          >
            添加面经
          </el-button>
        </div>
      </template>
      <el-table :data="list" style="width: 100%">
        <el-table-column prop="stem" label="标题" width="400">
        </el-table-column>
        <el-table-column prop="creator" label="作者"> </el-table-column>
        <el-table-column prop="likeCount" label="点赞"> </el-table-column>
        <el-table-column prop="views" label="浏览"> </el-table-column>
        <el-table-column prop="createdAt" label="更新日期" width="180">
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template #default="{ row }">
            <div class="actions">
              <i class="el-icon-view" @click="openDrawer('view', row.id)"></i>
              <i
                class="el-icon-edit-outline"
                @click="openDrawer('edit', row.id)"
              ></i>
              <i class="el-icon-delete" @click="del(row.id)"></i>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[10, 15, 20, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 抽屉 -->
    <el-drawer
      :title="drawerTitle"
      :visible.sync="drawer"
      :before-close="handleClose"
      size="50% "
    >
      <el-form
        v-if="drawerType !== 'view'"
        :model="form"
        :rules="rules"
        ref="form"
        label-width="80px"
      >
        <el-form-item label="标题" prop="stem">
          <el-input v-model="form.stem"> </el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quill-editor
            v-model="form.content"
            @blur="$refs.form.validateField('content')"
          >
          </quill-editor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">立即创建</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
      <div v-else>
        <h5>{{ form.stem }}}</h5>
        <div v-html="form.content"></div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import {
  ArticleListAPI,
  addAtricleAPI,
  delAtricleAPI,
  articleAPI,
  updateArticleAPI
} from '@/api/article'
import { quillEditor } from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
export default {
  name: 'articlePage',
  components: { quillEditor },
  data () {
    return {
      current: 1,
      pageSize: 10,
      list: [],
      total: 0,
      drawer: false,
      drawerType: '', // 打开抽屉后的操作类型
      form: {
        stem: '',
        content: ''
      },
      rules: {
        stem: { required: true, message: '不能为空', triggle: blur },
        content: { required: true, message: '不能为空', triggle: blur }
      }
    }
  },
  created () {
    this.initData() // 调用获取列表的方法
  },
  methods: {
    async initData () {
      const res = await ArticleListAPI({
        current: this.current,
        pageSize: this.pageSize
      })
      this.list = res.data.rows
      this.total = res.data.total
      console.log(res)
    },
    async del (id) {
      // console.log('haahah')
      try {
        await delAtricleAPI(id)
        this.initData()
        this.$message.success('删除成功')
      } catch (e) {
        if (e.response) {
          this.$message.error(e.response.data.message)
        } else {
          this.$message.error('删除失败')
        }
      }
    },
    // 每页多少条发生变化
    handleSizeChange (val) {
      this.pageSize = val
      this.initData()
    },
    // 页面发生变化触发
    handleCurrentChange (val) {
      // console.log(val)
      this.current = val
      this.initData()
    },
    // 打开抽屉的方法
    async openDrawer (type, id) {
      console.log(type, id)

      this.drawerType = type
      this.drawer = true
      if (type !== 'add') {
        // 获取面经详情
        const res = await articleAPI(id)
        // console.log('hsdhdshdh')
        // console.log(res)
        this.form.content = res.data.content
        this.form.stem = res.data.stem
        this.form.id = res.data.id
      }
    },
    // 关闭抽屉之前执行的方法
    handleClose () {
      this.drawer = false
      // this.$refs.form?.resetFields() // 重置表单
      // delete this.form.id // 删除id(有问题)
      this.form = {
        stem: '',
        content: ''
      }
      console.log('关闭')
    },
    // 提交校验
    onSubmit () {
      this.$refs.form.validate(async valid => {
        if (valid) {
          let msg = ''
          if (this.drawerType === 'add') {
            msg = '增加'
          } else {
            msg = '修改'
          }
          try {
            // console.log(this.form)
            if (this.drawerType === 'add') {
              await addAtricleAPI(this.form)
            } else {
              await updateArticleAPI(this.form)
            }
            this.handleClose() // 关闭表单
            this.initData() // 重新渲染页面
            this.$message.success(msg + '成功')
          } catch (e) {
            if (e.response) {
              this.$message.error(e.response.data.message)
            } else {
              this.$message.error(msg + '失败')
            }
          }

          // 验证成功
          // if (this.drawerType === 'add') {
          //   try {
          //     // console.log(this.form)
          //     await addAtricleAPI(this.form)
          //     this.handleClose() // 关闭表单
          //     this.initData() // 重新渲染页面
          //     this.$message.success('添加成功')
          //   } catch (e) {
          //     if (e.response) {
          //       this.$message.error(e.response.data.message)
          //     } else {
          //       this.$message.error('添加失败')
          //     }
          //   }
          // } else if (this.drawerType === 'edit') {
          //   // 修改内容提交
          //   try {
          //     await updateArticleAPI(this.form)
          //     this.handleClose() // 关闭表单
          //     this.initData() // 重新渲染页面
          //     this.$message.success('修改成功')
          //   } catch (e) {
          //     if (e.response) {
          //       this.$message.error(e.response.data.message)
          //     } else {
          //       this.$message.error('修改失败')
          //     }
          //   }
          // }
        }
      })
    }
  },
  computed: {
    drawerTitle () {
      // console.log('hahahahahah')
      console.log(this.drawerType)
      if (this.drawerType === 'add') {
        return '添加页面'
      } else if (this.drawerType === 'view') {
        return '查看页面'
      } else {
        return '修改页面'
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
